//
//  ViewController.m
//  Home
//
//  Created by 新宝 on 2017/6/26.
//  Copyright © 2017年 新宝. All rights reserved.
//

#import "ViewController.h"
#import <HomeKit/HomeKit.h>
#import "CurrentRoomController.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)MyHomeKit *homeKit;
@property(nonatomic,strong)UITableView *homeTableView;
@property(nonatomic,strong)NSArray *homeArry;
@property(nonatomic,strong)HMHome *currentHome;
@end

@implementation ViewController
-(NSArray *)homeArry
{
    if(!_homeArry)
    {
        _homeArry=[[NSArray alloc]init];
    }
    return _homeArry;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    self.navigationController.navigationBar.hidden=NO;
}

-(void)loadView
{
    [super loadView];
    self.homeTableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];
    self.homeTableView.delegate=self;
    self.homeTableView.dataSource=self;
    [self.view addSubview:self.homeTableView];
    
//    UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 60, 30)];
//    [button setTitle:@"创建Home" forState:UIControlStateNormal];
//    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
//    [button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchDown ];
//    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
//    self.navigationController.navigationItem.leftBarButtonItem=barButtonItem;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.homeKit=[[MyHomeKit alloc]init];
    [self.homeKit initMyHomeKit];
    self.homeArry=self.homeKit.homeManager.homes;
    [self.homeTableView reloadData];
    
}
- (IBAction)creatHomeButton:(id)sender {
    UIAlertController *inputNameAlert=[UIAlertController alertControllerWithTitle:@"请输入新home的名字" message:@"请确保这个名字的唯一性" preferredStyle:UIAlertControllerStyleAlert];
    __weak typeof (self) weakSelf = self;
    
    [inputNameAlert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder=@"请输入新家的名字";
    }];
    
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:0 handler:^(UIAlertAction * _Nonnull action) {
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"确定" style:0 handler:^(UIAlertAction * _Nonnull action) {
        NSString *newName =inputNameAlert.textFields.firstObject.text;
        
        [self.homeKit addHome:newName block:^(NSString *name) {
            NSLog(@"-----888-------%@",name);
            if(weakSelf.homeKit.homeManager.homes.count)
            {
                NSLog(@"------有多少Home-------%@",weakSelf.homeKit.homeManager.homes);
                weakSelf.homeArry=weakSelf.homeKit.homeManager.homes;
                NSLog(@"-------数组Home--------%@",weakSelf.homeArry);
                [weakSelf.homeTableView reloadData];
            }

        }];
       
        
        
    }];
    [inputNameAlert addAction:action1];
    [inputNameAlert addAction:action2];
    [self presentViewController:inputNameAlert animated:YES completion:^{}];
}

#pragma mark --UITableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.homeArry.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=[[UITableViewCell alloc]init];
    HMHome *home=self.homeArry[indexPath.row];
    cell.textLabel.text=home.name;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CurrentRoomController *currentRoom=[[CurrentRoomController alloc]init];
    HMHome *home=self.homeArry[indexPath.row];
    currentRoom.currentHome=home;
    [self.navigationController pushViewController:currentRoom animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
